package com.kern.controller;

import com.kern.pojo.ResponseResult;
import com.kern.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by caoshi at 15:25 2021-07-25
 */
@RestController
public class ContenController {

    @Autowired
    private ContentService contentService;


    /**
     *  爬取数据接口
     *  http://localhost:9090/parse/Java
     *
     * 解析数据将数据存入 es
     * @param keyWords      关键字 Java
     * @return
     * @throws IOException
     */
    @GetMapping("/parse/{keyWords}")
    public ResponseResult parse(@PathVariable("keyWords") String keyWords) throws IOException {
        boolean success = contentService.parseContent(keyWords);
        return success ? ResponseResult.ok("解析成功") : ResponseResult.fail("解析失败");
    }



    @GetMapping("/search/{keyWords}/{pageNum}/{pageSize}/{highlighte}")
    public ResponseResult search(@PathVariable String keyWords,
                                 @PathVariable int pageNum,
                                 @PathVariable int pageSize,
                                 @PathVariable int highlighte) {

        System.out.println("keyWords = " + keyWords + ", pageNum = " + pageNum + ", pageSize = " + pageSize);

        List<Map<String, Object>> result = null;
        try {

            if (highlighte > 0) {
                result = contentService.searchByPageHighlight(keyWords, pageNum, pageSize);
            } else {
                result = contentService.searchByPage(keyWords, pageNum, pageSize);
            }

        } catch (IOException e) {
            e.printStackTrace();
            return ResponseResult.fail("搜索内容为空");
        }

        return ResponseResult.ok(result);
    }







}
